package cn.edu.hizee.gps.controller;

import cn.edu.hizee.gps.dao.*;
import cn.edu.hizee.gps.model.ActionNode;
import cn.edu.hizee.gps.model.Contact;
import cn.edu.hizee.gps.model.Product;
import cn.edu.hizee.gps.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;

/**
 * @author ZX
 */
@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {

    @Autowired
    private ActionNodeRepository actionNodeRepository;

    @Autowired
    private SiteRepository siteRepository;

    @Autowired
    private Site2Repository site2Repository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ContactRepository contactRepository;

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private UnusualRepository unusualRepository;

    @Autowired
    private ProductRepository productRepository;

    private String NAME = "Excel导入";

    @PostMapping("find_alarm_chart")
    public String actionNode(HttpServletResponse response) {
        HashMap map = new HashMap<>();
        int nums_site[] = new int[7];
        int nums_site2[] = new int[7];
        map.put("userNum", userRepository.count());
        map.put("alarmNum", siteRepository.count());
        map.put("noSaveNum", site2Repository.count());
        List<Map<String, Object>> list_site_series = siteRepository.findCountAndExtra();
        List<Map<String, Object>> list_site2_series = site2Repository.findCountAndExtra();
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        for (int i = 0; i < list_site_series.size(); i++) {
            nums_site[Integer.valueOf(list_site_series.get(i).get("extra").toString()) - 1] = Integer.valueOf(list_site_series.get(i).get("COUNT(*)").toString());
        }

        for (int i = 0; i < list_site2_series.size(); i++) {
            nums_site2[Integer.valueOf(list_site2_series.get(i).get("extra").toString()) - 1] = Integer.valueOf(list_site2_series.get(i).get("COUNT(*)").toString());
        }

        for(int i = 0; i < nums_site.length; i++){
            nums_site[i] = nums_site[i] + nums_site2[i];
        }

        return ajaxReturn(response, 1, "success", list, nums_site);
    }


    @RequestMapping("list")
    public String list(HttpServletRequest request, HttpServletResponse response, String type, Model model)  {
        if(request.getMethod().equalsIgnoreCase("POST")){
            List<Map> dangerPeople = siteRepository.findAllDangerPeople();
            return ajaxReturn(response,dangerPeople);
        }
        else {
            if(type == null){
                type = NAME;
            }
            if(type.equals("user_list")){
                List<User> userList = userRepository.findPages(0,7);
                model.addAttribute("userList",userList);
                List<Contact> contactList = contactRepository.findPages(0,7);
                model.addAttribute("contactList",contactList);
            }
            if(type.equals("danger_list")){
                List<Map> dangerPeoples = siteRepository.findDangerPeoplePage(0,7);
                if(dangerPeoples.size() != 0) {
                    model.addAttribute("dangerList", dangerPeoples);
                }
            }
            if(type.equals("no_save_list")){
                List<Map> dangerPeoples = site2Repository.findDangerPeoplePage(0,7);
                if(dangerPeoples.size() != 0) {
                    model.addAttribute("noSaveList", dangerPeoples);
                }
            }
            if(type.equals("device_list")){
                List<Map> dangerPeoples = deviceRepository.findAllDevAndUser();
                if(dangerPeoples.size() != 0) {
                    model.addAttribute("devList", dangerPeoples);
                }
            }
            if(type.equals("unusual_list")){
                List<Map> unusuals = unusualRepository.findAllUnusualAndUser();
                if(unusuals.size() != 0) {
                    model.addAttribute("unList", unusuals);
                }
            }
            if(type.equals("shop_list")){
                List<Product> productList = productRepository.findAll();
                model.addAttribute("productList",productList);
            }
            model.addAttribute("fregUrl",type);
            return "admin/list :: " + type;
        }
    }

    @GetMapping("/page_list")
    public String pageList(HttpServletRequest request, HttpServletResponse response, String type, @RequestParam(defaultValue = "1")int pageNum){
        /*一页多少条*/
        int pageAmount = 7;
        if(type.equals("user_list")){
            List<User> userList = userRepository.findPages((pageNum - 1) * pageAmount, pageAmount);
            return ajaxReturn(response, userList);

        }
        if(type.equals("contact_list")){
            List<Contact> contactList = contactRepository.findPages((pageNum - 1) * pageAmount, pageAmount);
            return ajaxReturn(response, contactList);
        }
        if(type.equals("danger_list")){
            List<Map> dangerPeoples = siteRepository.findDangerPeoplePage((pageNum - 1) * pageAmount, pageAmount);
            return ajaxReturn(response, dangerPeoples);
        }
        if(type.equals("no_save_list")) {
            List<Map> dangerPeoples = site2Repository.findDangerPeoplePage((pageNum - 1) * pageAmount, pageAmount);
            return ajaxReturn(response, dangerPeoples);
        }
        return null;
    }

    @PostMapping("/upload")
    public String upload(MultipartFile fileUpload, HttpServletResponse response, HttpServletRequest request){
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名
        fileName = UUID.randomUUID()+suffixName;
        //指定本地文件夹存储图片
        String basePath = request.getServletContext().getRealPath("static/assets/test");
//        String filePath = "D:\\homework\\JSP\\JAVAEE\\gps\\src\\main\\resources\\static\\assets\\test\\";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(basePath+fileName));
            return ajaxReturn(response,1,"",null);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxReturn(response,0,"",null);
        }
    }

}
